﻿using System;
using System.Collections.ObjectModel;
using System.Web.UI.WebControls;

using Sunyi.Project.WebPoint.BusinessLogic;
using Sunyi.Project.WebPoint.Common;
using Sunyi.Project.WebPoint.DataContract;
using Sunyi.Project.WebPoint.WebControl;

namespace ActivitySystem.Web.Admin
{
    public partial class UserList : WebPointPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    BindRoles();
                    Search();
                }
                catch (Exception ex)
                {
                    ExceptionHandler.Handle(this, ex);
                }
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                gvUsers.PageIndex = 0;
                Search();
            }
            catch (Exception ex)
            {
                ExceptionHandler.Handle(this, ex);
            }
        }

        protected void gvUser_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {
                gvUsers.PageIndex = e.NewPageIndex;
                Search();
            }
            catch (Exception ex)
            {
                ExceptionHandler.Handle(this, ex);
            }
        }

        #region Private Method

        private void BindRoles()
        {
            RoleManager roleManager = new RoleManager();
            Collection<Role> roles = roleManager.SelectAll();

            ddlRole.Items.Clear();
            ddlRole.Items.Add(new ListItem(WebPointGlobalConstant.All, string.Empty));

            foreach (Role role in roles)
            {
                if (role.IsActive) ddlRole.Items.Add(new ListItem(role.RoleName, role.RoleName));
            }
        }

        private void Search()
        {
            string userName = tbxUserName.Text;
            string roleName = ddlRole.SelectedValue;
            bool isActive = string.IsNullOrEmpty(ddlStatus.SelectedValue) ? true : Boolean.Parse(ddlStatus.SelectedValue);

            UserManager userManager = new UserManager();

            if (gvUsers.PageIndex == 0)
            {
                gvUsers.TotalCount = userManager.SearchCount(userName, roleName, isActive);
            }

            gvUsers.DataSource = userManager.Search(userName, roleName, isActive, gvUsers.PageIndex, gvUsers.PageSize);
            gvUsers.DataBind();

            if (!Page.IsPostBack)
            {
                AuditLogger.Write(AuditLogOperationCode.Search, this.GetType().Name, userName, roleName, isActive, gvUsers.PageIndex, gvUsers.PageSize);
            }
        }

        #endregion

        #region WebPointPage Member

        protected override void ApplyPermission()
        {
            if (!this.HasPermission(PermissionType.Create))
            {
                litCreateNew.Visible = false;
            }

            if (!this.HasPermission(PermissionType.Update))
            {
                if (gvUsers.Columns.Count >= 1) gvUsers.Columns[gvUsers.Columns.Count - 1].Visible = false;
            }
        }

        #endregion
    }
}